﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Common;
using CoolERP_Entities;

namespace CoolERP_Web.User
{
    public partial class SK : BasePage
    {
        private double zero
        {
            get { return this.ViewState["zero"] == null ? 0 : Convert.ToDouble(this.ViewState["zero"]); }
            set { this.ViewState["zero"] = value; }
        }
        double ysze = 0, yishouze = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDDLs();
            }
        }
        //初始化客户简称和收入银行下拉框
        private void BindDDLs()
        {

            //收入银行下拉框
            using (ZL_YHZHBLL cB = new ZL_YHZHBLL())
            {
                this.ddlSRYH.Items.Clear();
                this.ddlSRYH.DataSource = cB.GetAll();
                this.ddlSRYH.DataValueField = "ID";
                this.ddlSRYH.DataTextField = "YHMC";
                this.ddlSRYH.DataBind();
                this.ddlSRYH.Items.Insert(0, "请选择");
            }
        }
        ////为页面绑定要显示的相关数据
        //private void BindData()
        //{
        //    if (ddlYSKZDBH.SelectedIndex == 0)
        //    {
        //        ShowMessage("请选择应收款帐单编号");
        //        return;
        //    }
        //    CW_YSKZDBLL cB = new CW_YSKZDBLL();
        //    CW_YSKZD entity = null;
        //    try
        //    {
        //        entity = cB.GetEntity(t => t.ID == Guid.Parse(ddlYSKZDBH.SelectedValue));
        //    }
        //    catch (Exception ex)
        //    {
        //        ShowMessage(ex.Message);
        //    }
        //    if (entity != null)
        //    {
        //        ysze = Convert.ToDouble(entity.YSZE);
        //        yishouze = Convert.ToDouble(entity.YISHOUZE);
        //        tbYSZE.Text = ysze.ToString();
        //        tbYISHOUZE.Text = yishouze.ToString();
        //        Label1.Text = zero + "---" + (yishouze - ysze);
        //    }

        //}

        //protected void ddlKHJC_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    if (ddlKHJC.SelectedValue != null)
        //    {
        //        XSHTBLL xB = new XSHTBLL();
        //        List<Guid> ids = xB.GetList(t => t.KHID == Guid.Parse(ddlKHJC.SelectedValue)).Select(t => t.ID).ToList();
        //        using (CW_YSKZDBLL cB = new CW_YSKZDBLL())
        //        {
        //            List<CW_YSKZD> yskzdList = cB.GetList(t => ids.Contains(t.XSHTID)&&t.ZDZT==0);
        //            if (yskzdList.Count > 0)
        //            {
        //                this.ddlYSKZDBH.Items.Clear();
        //                this.ddlYSKZDBH.DataSource = yskzdList;
        //                this.ddlYSKZDBH.DataValueField = "ID";
        //                this.ddlYSKZDBH.DataTextField = "BH";
        //                this.ddlYSKZDBH.DataBind();
        //                this.ddlYSKZDBH.Items.Insert(0, "请选择");
        //            }
        //        }

        //    }
        //}

        //protected void ddlYSKZDBH_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    BindData();
        //}

        ////保存收款单
        //protected void btnSave_Click(object sender, ImageClickEventArgs e)
        //{
        //    bool isAdd = true;
        //    bool result = false;
        //    if (string.IsNullOrEmpty(tbZPHM.Text.Trim()))
        //    {
        //        ShowMessage("请输入支票号码");
        //        return;
        //    }
        //    if (string.IsNullOrEmpty(tbSSJE.Text.Trim()))
        //    {
        //        ShowMessage("请输入实收金额");
        //        return;
        //    }
        //    double ssje = Convert.ToDouble(tbSSJE.Text.Trim());
        //    double region = Convert.ToDouble(tbYSZE.Text.Trim()) - Convert.ToDouble(tbYISHOUZE.Text.Trim());
        //    if (ssje <= zero || ssje > region)
        //    {
        //        ShowMessage("实收金额只能在给定范围内，请重新输入！");
        //        return;
        //    }
        //    CW_SKDBLL cB = new CW_SKDBLL();
        //    CW_SKD entity = new CW_SKD();
        //    if (ddlYSKZDBH.SelectedValue != null)
        //    {
        //        entity.ID = Guid.NewGuid();
        //        ZL_YWLSH ywItem = new ZL_YWLSH();
        //        entity.BH = GenYWLSH.GenBH(EnumHelper.GetDescriptionByName(typeof(EnumYWDJLX), EnumYWDJLX.SKD), out ywItem, out isAdd);
        //        //entity.YSKZDID = Guid.Parse(ddlYSKZDBH.SelectedValue);
        //        entity.SRYHID = Guid.Parse(ddlSRYH.SelectedValue);
        //        entity.ZPHM = this.tbZPHM.Text.Trim();
        //        entity.DZRQ = DateTime.Now;
        //        entity.SSJE = Convert.ToDouble(this.tbSSJE.Text.Trim());
        //        entity.JBRID = this.currentUser.UserID;
        //        try
        //        {
        //            result = cB.Insert(entity);
        //        }
        //        catch (Exception ex)
        //        {
        //            ShowMessage(ex.Message);
        //        }
        //        CW_YSKZDBLL cB1 = new CW_YSKZDBLL();
        //        CW_YSKZD entity1 = null;
        //        try
        //        {
        //            entity1 = cB1.GetEntity(t => t.ID == Guid.Parse(ddlYSKZDBH.SelectedValue));
        //        }
        //        catch (Exception ex)
        //        {
        //            ShowMessage(ex.Message);
        //        }
        //        if (entity1 != null)
        //        {
        //            entity1.YISHOUZE += entity.SSJE;
        //            if (entity1.YISHOUZE == entity1.YSZE)
        //                entity1.ZDZT = 1;
        //            try
        //            {
        //                result = result && cB1.Update(entity1);
        //            }
        //            catch (Exception ex)
        //            {
        //                ShowMessage(ex.Message);
        //            }
        //        }
        //    }
        //    if (result)
        //        lbResult.Text = "本次收款成功";
        //    else lbResult.Text = "本次收款失败";
        //}
        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            //保存收款单
            bool isAdd = true;
            bool result = false;
            CW_SKDBLL cB = new CW_SKDBLL();
            CW_SKD entity = new CW_SKD();
            entity.ID = Guid.NewGuid();
            ZL_YWLSH ywItem = new ZL_YWLSH();
            //entity.BH = GenYWLSH.GenBH(EnumHelper.GetDescriptionByName(typeof(EnumYWDJLX), EnumYWDJLX.SKD), out ywItem, out isAdd);
            //entity.YSKZDID = Guid.Parse(ddlYSKZDBH.SelectedValue);
            entity.SRYHID = Guid.Parse(ddlSRYH.SelectedValue);
            entity.ZPHM = this.tbZPHM.Text.Trim();
            entity.DZRQ = DateTime.Now;
            entity.SSJE = Convert.ToDouble(this.tbSSJE.Text.Trim());
            entity.JBRID = this.currentUser.UserID;
            try
            {
                result = cB.Insert(entity);
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }

            //更新应收款帐单表，即已收总额要加上这次收款的分配金额
            string[] yskzdIds = hidYSKZDID.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            string[] fpjes = hidFPJE.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            CW_YSKZDBLL cB1 = new CW_YSKZDBLL();
            CW_SKD_FPBBLL cB2 = new CW_SKD_FPBBLL();
            for (int i = 0; i < yskzdIds.Count(); i++)
            {
                CW_YSKZD entity1 = new CW_YSKZD();
                entity1 = cB1.GetEntity(t => t.ID == Guid.Parse(yskzdIds[i]));
                if (entity1 != null)
                {
                   entity1.YISHOUZE += Convert.ToDouble(fpjes[i]);
                    if (entity1.YISHOUZE == entity1.YSZE)
                        entity1.ZDZT = 1;
                        result = result && cB1.Update(entity1);
                }
                //向收款单分配表插入数据
                CW_SKD_FPB entity2 = new CW_SKD_FPB();
                entity2.ID = Guid.NewGuid();
                entity2.YSKZDID = Guid.Parse(yskzdIds[i]);
                entity2.SKDID = entity.ID;
                entity2.FPJE = Convert.ToDouble(fpjes[i]);
                result = result && cB2.Insert(entity2);
            }
            if (result)
            {
                btnSave.Visible = false;
                labResult.Text = "保存成功";
                using (ZL_YWLSHBLL zBLL = new ZL_YWLSHBLL())
                {
                    if (ywItem.ID != Guid.Empty)
                    {
                        if (isAdd)
                            zBLL.Insert(ywItem);
                        else
                            zBLL.Update(ywItem);
                    }
                }
            }
            else
                labResult.Text = "保存失败";
        }
    }
}